package com.wbc.smartHome.service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wbc.smartHome.dao.LiveTVDao;
import com.wbc.smartHome.model.LiveTV;
import com.wbc.smartHome.model.Result;
import com.wbc.smartHome.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class LiveTVService {

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LiveTVDao liveTVDao;
    private String key = "livetv";

    public PageInfo<LiveTV> getLiveTVList(LiveTV liveTV) {
        /*ListOperations listOps  = redisTemplate.opsForList();
        List<LiveTV> liveTVList = listOps.range(key, 0, -1);*/
        PageHelper.startPage(liveTV.getPageNo(), liveTV.getPageSize());
        List<LiveTV> liveTVList = liveTVDao.getLiveTVList();
        PageInfo<LiveTV> pageInfo = new PageInfo<>(liveTVList);
        return pageInfo;
    }


    public void setLiveTV(LiveTV liveTV, String adminToken) {
     /* ListOperations listOps  = redisTemplate.opsForList();
        listOps.rightPush(key, liveTV);*/
        int adminId = JWTUtil.getAdmin(adminToken).getId();
        liveTVDao.setLiveTVList(liveTV.getName(),liveTV.getImg(),liveTV.getUrl(),liveTV.getStartTime(),adminId);
    }

    public void updateLiveTV(LiveTV liveTV, String adminToken) {
        int adminId = JWTUtil.getAdmin(adminToken).getId();
        liveTVDao.updateLiveTV(liveTV.getId(),liveTV.getName(),liveTV.getImg(),liveTV.getUrl(),liveTV.getStartTime(),adminId);
    }


    public Result uploadImg(CommonsMultipartFile commonsMultipartFile) throws IOException {
        //为了避免上传的文件重名,重新生成一个文件名
        String newFileName = new Date().getTime()+commonsMultipartFile.getOriginalFilename();
        //连接oss
        OSS ossClient = new OSSClientBuilder().build("https://"+endpoint, accessKeyId, accessKeySecret);
        //长传文件
        ossClient.putObject(bucketName, newFileName, commonsMultipartFile.getInputStream());
        //拼接文件在oss中访问地址
        String url = "https://"+bucketName+"."+endpoint+"/"+newFileName;
        //newsDao.savePhoto(url);
        return new Result(200,"操作成功",url);
    }

    public void delete(int id) {
        liveTVDao.delete(id);
    }

    public LiveTV getLiveTVById(int id) {
        LiveTV liveTV =liveTVDao.getLiveTVById(id);
        return liveTV;
    }
}
